package com.xudow.app.newui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.newui.base.BaseUiActivity;
import com.xudow.app.newui.zutils.ZPreferenceUtils;
import com.xudow.app.ui.task.UserSecretUpDataTask;

import java.util.HashMap;

import butterknife.BindView;

/**
 * 作者：zhouXJ on 2016/4/7 10:32
 * 消息推送
 */
public class MessagePushActivity extends BaseUiActivity implements View.OnClickListener {

    @BindView(R.id.message_push_comment_img)
    ImageView message_push_comment_img;

    @BindView(R.id.message_push_praise_img)
    ImageView message_push_praise_img;

    @BindView(R.id.message_push_fans_img)
    ImageView message_push_fans_img;

    @BindView(R.id.message_push_system_img)
    ImageView message_push_system_img;

    @BindView(R.id.message_push_schedule_img)
    ImageView message_push_schedule_img;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_push);

    }

    public void onBackBtnClick(View view) {
        finish();
    }

    @Override
    protected void initData() {
        initBtnState();
    }

    @Override
    protected void initListener() {
        message_push_comment_img.setOnClickListener(this);
        message_push_praise_img.setOnClickListener(this);
        message_push_fans_img.setOnClickListener(this);
        message_push_system_img.setOnClickListener(this);
        message_push_schedule_img.setOnClickListener(this);
    }

    private boolean isFansChecked = false;
    private boolean isCommentChecked = false;
    private boolean isPraiseChecked = false;
    private boolean isSystemChecked = false;
    private boolean isScheduleChecked = false;
    private String getStatu(boolean b){
        if (b)
            return "1";
        else
            return "0";
    }
    @Override
    public void onClick(View view) {
        initBtnState();
        String url="";
        String key="status";
        String pramer="";
        switch (view.getId()) {

            case R.id.message_push_comment_img: // 评论
                /*isCommentChecked =!isCommentChecked;
                if (isCommentChecked) { // 开
                    message_push_comment_img.setImageResource(R.mipmap.btn_switch_p);
                } else { // 关
                    message_push_comment_img.setImageResource(R.mipmap.btn_switch_c);
                }*/
                url= Config.PUSH_WITH_BECOMMENTED;
                pramer=getStatu(!isCommentChecked);
                //ZPreferenceUtils.saveSwitchBtnState(this, ZPreferenceUtils.PREF_COMMENT, isCommentChecked);

                tag=ZPreferenceUtils.PREF_COMMENT;
                value=isCommentChecked;
                swtch=message_push_comment_img;
                break;

            case R.id.message_push_praise_img: // 赞
               /* isPraiseChecked =!isPraiseChecked;
                if (isPraiseChecked) { // 开
                    message_push_praise_img.setImageResource(R.mipmap.btn_switch_p);
                } else { // 关
                    message_push_praise_img.setImageResource(R.mipmap.btn_switch_c);
                }*/
                url=Config.PUSH_WITH_BELIKED;
                pramer=getStatu(!isPraiseChecked);
                //ZPreferenceUtils.saveSwitchBtnState(this, ZPreferenceUtils.PREF_PRAISE, isPraiseChecked);
                tag=ZPreferenceUtils.PREF_PRAISE;
                value=isPraiseChecked;
                swtch=message_push_praise_img;
                break;

            case R.id.message_push_fans_img: // 粉丝
                /*isFansChecked =!isFansChecked;
                if (isFansChecked) { // 开
                    message_push_fans_img.setImageResource(R.mipmap.btn_switch_p);
                } else { // 关
                    message_push_fans_img.setImageResource(R.mipmap.btn_switch_c);
                }*/
                url=Config.PUSH_WITH_BEATTENTIONED;
                pramer=getStatu(isFansChecked);
                //ZPreferenceUtils.saveSwitchBtnState(this, ZPreferenceUtils.PREF_FANS, isFansChecked);
                tag=ZPreferenceUtils.PREF_FANS;
                value=isFansChecked;
                swtch=message_push_fans_img;
                break;

            case R.id.message_push_system_img: // 系统通知
               /* isSystemChecked =!isSystemChecked;
                if (isSystemChecked) { // 开
                    message_push_system_img.setImageResource(R.mipmap.btn_switch_p);
                } else { // 关
                    message_push_system_img.setImageResource(R.mipmap.btn_switch_c);
                }*/
                url=Config.PUSH_WITH_SYSTEMNOTICE;
                pramer=getStatu(isSystemChecked);
               // ZPreferenceUtils.saveSwitchBtnState(this, ZPreferenceUtils.PREF_SYSTEM, isSystemChecked);
                tag=ZPreferenceUtils.PREF_SYSTEM;
                value=isSystemChecked;
                swtch=message_push_system_img;
                break;

            case R.id.message_push_schedule_img: // 上课提醒
                /*isScheduleChecked =!isScheduleChecked;
                if (isScheduleChecked) { // 开
                    message_push_schedule_img.setImageResource(R.mipmap.btn_switch_p);
                } else { // 关
                    message_push_schedule_img.setImageResource(R.mipmap.btn_switch_c);
                }*/
                url=Config.PUSHI_WITH_COURSE;
                pramer=getStatu(isScheduleChecked);
                //ZPreferenceUtils.saveSwitchBtnState(this, ZPreferenceUtils.PREF_SCHEDULE, isScheduleChecked);
                tag=ZPreferenceUtils.PREF_SCHEDULE;
                value=isScheduleChecked;
                swtch=message_push_schedule_img;
                break;
        }
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("url",url);
        map.put("key", key);
        map.put("pramer", pramer);
        UserSecretUpDataTask upDataTask=new UserSecretUpDataTask(MessagePushActivity.this,upDataHandler);
        addTask(upDataTask);
        upDataTask.execute(map);
    }
    /**
     *开关handler
     */
    private Handler upDataHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==UserSecretUpDataTask.SUCCESS){
                getMyApp().makeToast("开关成功");
                saveEdit(tag,value,swtch);
            }else {
                String str=msg.getData().getString("errMessage");
                if (null==str)
                    str="开关不成功";
                getMyApp().makeToast(str);
            }
        }
    };

    String tag;
    boolean value;
    ImageView swtch;
    private void saveEdit(String spTag,boolean value,ImageView swtch){
        value=!value;
        if (value){//开
            swtch.setImageResource(R.mipmap.btn_switch_p);
        } else { // 关
            swtch.setImageResource(R.mipmap.btn_switch_c);
        }
        ZPreferenceUtils.saveSwitchBtnState(this, spTag, value);
    }
    
    /**
     * 初始化选中按钮状态
     */
    private void initBtnState() {
        isCommentChecked = ZPreferenceUtils.getSwitchBtnState(this, ZPreferenceUtils.PREF_COMMENT);
        if (isCommentChecked) {
            message_push_comment_img.setImageResource(R.mipmap.btn_switch_p);
        } else {
            message_push_comment_img.setImageResource(R.mipmap.btn_switch_c);
        }

        isPraiseChecked = ZPreferenceUtils.getSwitchBtnState(this, ZPreferenceUtils.PREF_PRAISE);
        if (isPraiseChecked) {
            message_push_praise_img.setImageResource(R.mipmap.btn_switch_p);
        } else {
            message_push_praise_img.setImageResource(R.mipmap.btn_switch_c);
        }

        isFansChecked = ZPreferenceUtils.getSwitchBtnState(this, ZPreferenceUtils.PREF_FANS);
        if (isFansChecked) {
            message_push_fans_img.setImageResource(R.mipmap.btn_switch_p);
        } else {
            message_push_fans_img.setImageResource(R.mipmap.btn_switch_c);
        }

        isSystemChecked = ZPreferenceUtils.getSwitchBtnState(this, ZPreferenceUtils.PREF_SYSTEM);
        if (isSystemChecked) {
            message_push_system_img.setImageResource(R.mipmap.btn_switch_p);
        } else {
            message_push_system_img.setImageResource(R.mipmap.btn_switch_c);
        }

        isScheduleChecked = ZPreferenceUtils.getSwitchBtnState(this, ZPreferenceUtils.PREF_SCHEDULE);
        if (isScheduleChecked) {
            message_push_schedule_img.setImageResource(R.mipmap.btn_switch_p);
        } else {
            message_push_schedule_img.setImageResource(R.mipmap.btn_switch_c);
        }
    }
}
